#下面做出彩色图像的中值滤波
import numpy as np
from scipy import ndimage
from skimage import data,util,io
from matplotlib import pyplot as plt
# img为原始图像(有色彩的)
#img = data.astronaut()
img = io.imread(fname = '和平精英.jpg')
noise_img = np.zeros(img.shape) #构造和img一样大小的数组形状
new_img = np.zeros(img.shape) #构造和img一样大小的数组形状
for i in range(3): #3是0,1,2分别表示红绿蓝通道图像
    graying = io.imread(fname = '和平精英.jpg')[:,:,i]
    #对图像加椒盐噪声
    noise_img[:,:,i] = util.random_noise(graying,mode = 's&p',seed = None,clip = True)
    #中值滤波
    n = 3
    new_img[:,:,i] = ndimage.median_filter(noise_img[:,:,i],(n,n))
#显示图像
plt.figure()
plt.imshow(img,cmap = 'gray') #显示原始图像
plt.figure()
plt.imshow(noise_img,cmap = 'gray') #显示加噪图像
plt.figure()
plt.imshow(new_img,cmap = 'gray') #显示降噪图像
